/*************************************************************************
    > File Name: 1.pthread_attr_init.c
    > Author: Ykk
    > Mail: 3393579535@qq.com 
    > Created Time: 2023年05月10日 星期三 20时21分48秒
 ************************************************************************/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#include<unistd.h>
#include<pthread.h>

void *fun(void *arg)
{
	int i = 0;
	for(i = 0;i < 5;i++)
	{
		printf("pthread do working...%d\n",i);
		sleep(1);
	}
	pthread_exit(NULL);
}
int main()
{
	int ret = -1;
	pthread_t tid = -1;
	pthread_attr_t attr;
	//初始化线程属性
	ret = pthread_attr_init(&attr);
	if(0 != ret)
	{
		printf("pthread_attr_init failed...\n");
		return 1;
	}
	printf("线程属性初始化函数ok....\n");
	//设置线程为分离状态
	ret = pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
	if(0 != ret)
	{
		printf("pthread_attr_setdetachstate failed....\n");
		return 1;
	}

	//创键一个线程 使用初始化好的属性
	ret = pthread_create(&tid, &attr,fun,NULL);
	if(0 != ret)
	{
		printf("pthread_create failed...\n");
		return 1;
	}
	printf("main thread...tid:%lu\n",pthread_self());

	//测试当前线程是否为分离状态
	ret = pthread_join(tid,NULL);
	if(0 != ret)
	{
		printf("当前线程为分离状态.....\n");
	}
	else
	{
		printf("当前线程为非分离状态....\n");
	}
	
	//销毁线程属性
	ret = pthread_attr_destroy(&attr);
	if(0 != ret)
	{
		printf("pthread_attr_destory failed....\n");
		return 0;
	}

	printf("按下任意键退出...\n");
	getchar();
	return 0;
}
